package cn.kgc.springmvc00.controller;

import cn.kgc.springmvc00.pojo.User;
import com.alibaba.fastjson.JSON;
import net.sf.jsqlparser.expression.JsonExpression;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.security.PublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shkstart
 * @create 2019-12-19 14:52
 */
@Controller
@RequestMapping("/user")
public class TestController {

    @RequestMapping("/index.do")
    public ModelAndView index(){
        return new ModelAndView("index");
    }

    @RequestMapping("/add.do")
    public ModelAndView index02(String name){
        System.out.println("欢迎:"+name);
        return null;
    }

    @RequestMapping(value="/add3.do",method= RequestMethod.POST)
    public ModelAndView index04(@RequestParam("name") String name,@RequestParam("age") int age){
        System.out.println("欢迎:"+name+"\t年龄:"+age);
        return null;
    }

    @RequestMapping(value="/add4.do",method= RequestMethod.GET)
    public ModelAndView index05( String name){
        System.out.println("欢迎:"+name);
        return null;
    }

    @RequestMapping("/toregister.do")
    public String toregister(){

        return "toregister";
    }

   /* @RequestMapping(value="/register.do",method=RequestMethod.POST)
    public String register(User user){
        System.out.println(user.toString());
        return "index";
    }*/

    /*@RequestMapping(value="/register.do",method=RequestMethod.POST)
    public ModelAndView register01(User user){
        ModelAndView mav=new ModelAndView("index");
        mav.addObject("name",user.getName());
        mav.addObject("user",user);
        return mav;
    }*/

   /* @RequestMapping(value="/register.do",method=RequestMethod.POST)
    public String register(User user,Model model){
        System.out.println(user.toString());
        model.addAttribute("name",user.getName());
        model.addAttribute("user",user);
        return "index";
    }*/

    /*@RequestMapping(value="/register.do",method=RequestMethod.POST)
    public String register(User user,Model model){
        System.out.println(user.toString());
        model.addAttribute(user.getName());
        model.addAttribute(user);
        return "index";
    }*/

    @RequestMapping(value="/register.do",method=RequestMethod.POST)
    public String register(User user,Map<String,Object>map){
        System.out.println(user.toString());
        map.put("name",user.getName());
        map.put("user",user);
        return "index";
    }
    @RequestMapping(value = "/text.do",produces = {"application/json;charset=utf-8"})
    @ResponseBody
    public  String test(){
        Map<String,Object>map=new HashMap<>();
        map.put("msg","成功");
        return JSON.toJSONString(map);
    }
    @RequestMapping("/test2.do")
    public  String test02(Map<String,Object>map){
        map.put("msg","成功");
        return "redirect:http://www.baidu.com";
    }
}
